<?php

class ArticleController extends Controller
{


	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + Comment', // we only allow deletion via POST request
		);
	}

	public function actions() 
    { 
            return array( 
                    'captcha'=>array( 
                            'class'=>'CCaptchaAction', 
                            'backColor'=>0xFFFFFF, 
                            'minLength'=>5,
                            'maxLength'=>5,
                            'width'=>70,
                            'height'=>50
                    ), 
            ); 
    } 

	public function actionView($id)
	{
		$model  =$this->loadModel($id);
		$sql ="update edu_article set views=views+1 where id=:id";
		Yii::app()->db->createCommand($sql)->bindParam(":id",$id)->execute();
		$this->render('view',
				array('model'=> $model
			));
	}


		/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Article the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		// $cachekey= "web-article-model-".$id;
		// $model= Yii::app()->cache->get($cachekey);
		// if ($model==false && $id>0) {
		// 	$model=Article::model()->findByPk($id);
		// 	Yii::app()->cache->set($cachekey,$model);
		// }
		$model=Article::model()->findByPk($id);
		if($model===null || $model==false)
			throw new CHttpException(404,'对不起,您查询的内容不存在');
		return $model;
	}


	public function actionComment()
	{
		$status=200;
		$message="";
		$form= new CommentForm;
		if (isset($_POST)) {
			$form->attributes = $_POST;
			if ($form->validate()) {
				$model = new Comment;
				$model->parentid = $form->parentid;
				$model->content = $form->content;
				$model->save();
			}
			else{
				$status=400;
				$keys=array_keys($form->getErrors());
				$message=$form->getErrors($keys);
			}
		}
		echo json_encode(array('status'=>$status,"message"=>$message));
	}
}